package com.sanda.androidcourse.strategy.mvvm;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.sanda.androidcourse.R;

// Activity
public class CalculatorMVVMActivity extends AppCompatActivity {
    private CalculatorViewModel viewModel;
    EditText etNum1, etNum2;
    TextView tvResult;
    Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        etNum1 = findViewById(R.id.etNum1);
        etNum2 = findViewById(R.id.etNum2);
        tvResult = findViewById(R.id.tvResult);
        btnAdd = findViewById(R.id.btnAdd);

        viewModel = new ViewModelProvider(this).get(CalculatorViewModel.class);

        btnAdd.setOnClickListener(v -> {
            viewModel.num1.setValue(etNum1.getText().toString());
            viewModel.num2.setValue(etNum2.getText().toString());
            viewModel.add();
        });

        viewModel.result.observe(this, res -> tvResult.setText(res));
    }
}
